﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using AlarmManagePlatform.Model.DB;

namespace AlarmManagePlatform.DataAccess
{
    internal class tb_TaskDetails
    {
        internal List<Model.DB.tb_TaskDetails> get(string TaskId)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                string sqlScript = string.Format("select Id,TaskId,FactoryNumber from tb_TaskDetails  where TaskId = '{0}' order by FactoryNumber ", TaskId);
                sqlDb = Static.Methods.OpenDataBase();
                DataTable tempDT = sqlDb.RunSqlScript(sqlScript);
                List<Model.DB.tb_TaskDetails> result = new List<Model.DB.tb_TaskDetails>();
                Model.DB.tb_TaskDetails tb_TaskDetails;
                for (int i = 0; i < tempDT.Rows.Count; i++)
                {
                    tb_TaskDetails = new Model.DB.tb_TaskDetails();
                    tb_TaskDetails.Id = int.Parse(tempDT.Rows[i]["Id"].ToString());
                    tb_TaskDetails.TaskId = tempDT.Rows[i]["TaskId"].ToString();
                    tb_TaskDetails.FactoryNumber = tempDT.Rows[i]["FactoryNumber"].ToString();
                    result.Add(tb_TaskDetails);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取任务详情失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }

        internal void AddTaskDeatils(List<Model.DB.tb_TaskDetails> taskDetails)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _SqlScript = string.Empty;
            try
            {
                _SqlDb = Static.Methods.OpenDataBase();
                foreach (var item in taskDetails)
                {
                    _SqlScript = string.Empty;
                    _SqlScript += "Insert Into tb_TaskDetails (TaskId,FactoryNumber)";
                    _SqlScript += $"    values ('{item.TaskId}','{item.FactoryNumber}')";
                    _SqlDb.UpdateSqlScript(_SqlScript);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("添加任务详情失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal void DeleteForTaskID(List<string> ListTaskId)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _TaskIds = string.Empty;
            string _SqlScript = string.Empty;
            try
            {
                _SqlDb = Static.Methods.OpenDataBase();
                _TaskIds = Static.Methods.CombiningStrings1<string>(ListTaskId);

                _SqlScript = "Delete From tb_TaskDetails";
                _SqlScript += $" Where TaskId in ( {_TaskIds} )";

                _SqlDb.UpdateSqlScript(_SqlScript);
            }
            catch (Exception ex)
            {
                throw new Exception("删除任务详情失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal bool CheckIsExist(string factoryNumber)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _SqlScript = string.Empty;
            try
            {
                _SqlScript = $"Select Id from tb_TaskDetails where [FactoryNumber] = '{factoryNumber}'";
                _SqlDb = Static.Methods.OpenDataBase();
                if (_SqlDb.RunSqlScript(_SqlScript).Rows.Count > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw new Exception("检查报警器是否存在与TaskDetails表中失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }
    }
}